#include "mod_media.h"
#include "http.h"
#include "server.h"

std::string MediaModule::GetName() const
{
  return std::string("media");
}

Http MediaModule::ProcessRequest(const Http& request)
{
#ifdef _WIN32
  const char* file = "e:\\test.mp4";
#else
  const char* file = "/terra/test.mp4";
#endif

  std::string url = request.GetRequestURL();
  url.erase(1, GetName().size());
  if (url.size() > 0 && url[0] == '/')
    url.erase(0, 1);
  if (url.size() != 0)
  {
    int range1 = 0;
    int range2 = 1;
    request.GetRequestRange(&range1, &range2);
    Http response(Http::Response::PartialContent);
    response.AddHeader("Connection", "keep-alive");
    response.SetContentBinary(file, range1, range2);
    return response;
  }
  Http response(Http::Response::Ok);
  response.AddHeader("Connection", "close");
  std::string s;
  s += "<html><body><a href='http://";
  s += server->GetAddress();
  s += "/media/test.mp4'>test</a></body></html>\r\n";
  response.SetContentHTML(s);
  return response;
}